package cn.snailhd.core.base.interceptor;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.IOUtils;
import org.springframework.core.Conventions;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import com.alibaba.fastjson.JSON;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.PathNotFoundException;

import cn.snailhd.core.base.annotation.Token;
import cn.snailhd.core.base.dao.RedisDao;
import cn.snailhd.core.base.service.TokenService;
import cn.snailhd.core.base.utils.SpringContextUtil;


public class RequestParamResovler implements HandlerMethodArgumentResolver {

	
	
	private static final String JSON_REQUEST_BODY = "JSON_REQUEST_BODY";

	@Override
	public boolean supportsParameter(MethodParameter parameter) {
		
		return parameter.hasParameterAnnotation(Token.class);//返回是否使用给定的注释类型声明参数。
	}

	@Override
	public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
			NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
		
		
		String name = Conventions.getVariableNameForParameter(parameter);
		
		
        String body = getRequestBody(webRequest);
        String token = JSON.parseObject(body).getString("token");
        TokenService tokenService = SpringContextUtil.getBean(TokenService.class);
        String userid = tokenService.verifyToken(token, true);
        
        Object val = null;

//		try {
//            val = JsonPath.read(userid, parameter.getParameterAnnotation(Token.class).value());
////            if (parameter.getParameterAnnotation(Token.class).required() && val == null) {
////               // throw new ParamCheckException(parameter.getParameterAnnotation(Token.class).value() + "不能为空");
////            }
//        } catch (PathNotFoundException exception) {
//            System.out.println(exception.getStackTrace());
////            if (parameter.getParameterAnnotation(Token.class).required()) {
//////                throw new ParamCheckException(parameter.getParameterAnnotation(JsonParam.class).value() + "不能为空");
////                throw exception;
////            }
//        }
        return userid;

			
	}
	
	private String getRequestBody(NativeWebRequest webRequest) {
        HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
        String jsonBody = (String) servletRequest.getAttribute(JSON_REQUEST_BODY);
        if (jsonBody == null) {
            try {
                jsonBody = IOUtils.toString(servletRequest.getInputStream());
                servletRequest.setAttribute(JSON_REQUEST_BODY, jsonBody);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return jsonBody;

    }
	

	

}
